
//
//  ZYStudentDivisionController.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/8.
//  Copyright © 2018年 星道三好. All rights reserved.
//

#import "ZYStudentDivisionController.h"
#import "ZYStudentDivisionCell.h"
#import "ZYStudentDivisionModel.h"
#define kCell @"cell"
#import "HNDistributionAddStudent.h"
@interface ZYStudentDivisionController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isTrue;
@end

@implementation ZYStudentDivisionController


- (NSMutableArray *)dataSource{
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
    
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(51,51,51)}];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(255,255,255)}];
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];

    [SVProgressHUD dismiss];
}



- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"学生分班";
    self.view.backgroundColor = ColorRGB(245, 245, 245);

    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(pushNextAction) image:@"icon_add_class_default" highImage:@"icon_add_class_default"];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(backAction) image:@"icon_back_student_sign" highImage:@"icon_back_student_sign"];
    
    [self setUpSuperView];
    [self setUpRefresh];

}


- (void)backAction{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}



#pragma mark --- 添加方法的实现
- (void)pushNextAction{
    
    HNDistributionAddStudent *VC = [[HNDistributionAddStudent alloc] init];
    VC.Uid = (NSNumber *)self.teacherUid;
    VC.LessonID = (NSNumber *)self.lessonId;
    VC.isUp = YES;
    VC.PopRefresh = ^{
        [self.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:VC animated:YES];
    
}



- (void)setUpSuperView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    self.tableView.dataSource =self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = ColorRGB(247, 245, 245);
    self.tableView.estimatedRowHeight = 200;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[ZYStudentDivisionCell class] forCellReuseIdentifier:kCell];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.tableView.tableFooterView = [UIView new];
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);

}





#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
}

- (void)setNetWork{
//    http://192.168.0.113:8080/StarShine/ys/trainLesson.do?lessonId=855&teacherUid=255&action=getStudentList&pageNumber=1
    
    NSString *url = [NSString stringWithFormat:@"trainLesson.do?lessonId=%@&teacherUid=%@&action=getStudentList&pageNumber=%ld",self.lessonId,self.teacherUid,(long)self.page];
    
    
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        
        
        [SVProgressHUD dismiss];
        
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            
            NSDictionary *dict = [Obj valueForKey:@"t"];
            
            NSArray *arr = [dict valueForKey:@"dataList"];
            
            
            for (NSDictionary *dic in arr) {
                
               ZYStudentDivisionModel *model = [ZYStudentDivisionModel new];
                
                [model setValuesForKeysWithDictionary:dic];
                
                [self.dataSource addObject:model];
                
            }
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
            
            
        }
        
        
        
        
        
    } Fail:^(NSError *Error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
}




















































#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYStudentDivisionCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    if (self.dataSource.count > indexPath.row) {
        
        ZYStudentDivisionModel *model = self.dataSource[indexPath.row];
        
        [cell assignByZYStudentDivisionModel:model];
    }
    
    [cell.deleteButton addTarget:self action:@selector(deleteButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    return cell;
}

#pragma mark--- 删除方法的实现
- (void)deleteButtonAction:(UIButton *)sender{
    ZYStudentDivisionCell *cell = (ZYStudentDivisionCell *)sender.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYStudentDivisionModel *model = self.dataSource[indexPath.row];
    
    
    ZYalertController *alert = [ZYalertController alertControllerWithTitle:@"温馨提示" message:@"是否删除?" preferredStyle:(UIAlertControllerStyleAlert)];
    ZYAlertAction *action = [ZYAlertAction actionWithTitle:@"YES" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        if (_dataSource.count > 1) {
            
            
            NSMutableArray *temp = [NSMutableArray arrayWithArray:_dataSource];
            
            [temp removeObjectAtIndex:indexPath.row];
            
            _dataSource = temp;
            
            NSArray *deleteItems = @[indexPath];
            
            [self.tableView deleteRowsAtIndexPaths:deleteItems withRowAnimation:(UITableViewRowAnimationRight)];
            
        }else{
            [_dataSource removeAllObjects];
            [self.tableView reloadData];
        }
        
        
        [self netMoonWorkId:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",model.xtgpId]]];
        
        
    }];
    
    ZYAlertAction *actionn = [ZYAlertAction actionWithTitle:@"NO" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    action.textColor = [UIColor blackColor];
    actionn.textColor = [UIColor blackColor];
    [alert addAction:action];
    [alert addAction:actionn];
    [self presentViewController:alert animated:YES completion:nil];
    
    
    
    
    
    
    
}


#pragma mark --- 删除接口的实现
- (void)netMoonWorkId:(NSString *)xtgpId{
//    http://192.168.0.113:8080/StarShine/ys/trainLesson.do?id=137&action=delStudentById
    NSString *url = [NSString stringWithFormat:@"trainLesson.do?id=%@&action=delStudentById",xtgpId];
    
    
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            NSLog(@"删除成功");
        }
        
        
    } Fail:^(NSError *Error) {
        
    }];
    
    
    
    
}













@end
